<?php
namespace Application\Controller;

use Application\Model\Dao\GraphDao;
use Application\Model\Dao\UpdateDao;
use Application\Service\UpdateService;
use Zend\View\Model\JsonModel;

class InfoController extends LoginAwareController
{

    /**
     * @return JsonModel
     * Get all discussion data.
     */
    public function saveDirectoryAction()
    {
        $request = $this->getRequest();
        if ($request->isPost()) {
            $name = $request->getPost('name');
            $priority = intval( $request->getPost('priority'));
            $profession =  $request->getPost('profession');
            $year = intval( $request->getPost('year'));

            $node = array(
                'name' => $name,
                'priority' => $priority,
                'profession' => $profession,
                'year' => $year
            );
            $graph = new GraphDao();
            $graph->saveNode($node);
            $data = array(
                'result' => 'success'
            );
            return new JsonModel($data);
        }
    }


    /**
     * @return JsonModel
     * Get all discussion data.
     */
    public function getAction()
    {
        $request = $this->getRequest();
        $lastSeenId = $request->getQuery('lastSeenId');
        $after = $request->getQuery('after') == 'false' ? '' : true;
        $limit = $request->getQuery('limit');
        $updateDao = $this->getServiceLocator()->get('update');

        $result = $updateDao->getUpdateData($lastSeenId, $this->getLoggedinUserId(),
            array_filter(UpdateDao::$UPDATE_TYPE, function ($key){
                return $key === 2;
            }), $limit, $after);


        $updateService = new UpdateService();
        $finalResult = $updateService->processUpdate($result, $this->getLoggedinUserId());
        return new JsonModel($finalResult);
    }
}

?>